package com.gxuwz.lhleavesys.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import com.gxuwz.lhleavesys.context.AppContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.gxuwz.lhleavesys.entity.User;
import com.gxuwz.lhleavesys.service.UserService;
import com.gxuwz.lhleavesys.service.impl.UserServiceImpl;

@WebServlet(name = "settingServlet", urlPatterns = { "/user/setting" })
public class UserServlet extends BaseLHServlet {

    @Autowired
    private UserService userService;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            this.userService = (UserServiceImpl) AppContext.getBean("userServiceImpl", UserServiceImpl.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (action == null) {
            User user = null;
            try {
                user = userService.user();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            req.setAttribute("user", user);
            req.getRequestDispatcher("/views/admin/userSetting.jsp").forward(req, resp);
        } else if (action.equals("edit")) {
            updateUser(req, resp);
        } else if (action.equals("login")) {
            resp.setContentType("text/html;charset=UTF-8");
            String jsCode = "<script>window.top.location.href='UserServlet?action=login';</script>";
            resp.getWriter().write(jsCode);
        } else if (action.equals("logout")) {
            logout(req, resp);
        }
    }

    private void updateUser(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        user.setTelNumber(request.getParameter("telNumber"));
        try {
            userService.editUser(user);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        logout(request, response);
        /* this.dispatch(request,response,view_logout); */
    }

    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<script type=\"text/javascript\">");
        out.println("window.top.location.href='http://localhost:8080/lhleavesys_war/login';");
        out.println("</script>");
    }
}
